package com.jingzhi.huimiao.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * 
 * 数据存储类（存入sp）
 * 
 * @author cxk
 * 
 * 
 */
public class DataStoreUtil {
	public static String datastore = "data";

	/**
	 * 存入数据的方法
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void put(Context context, String key, String value) {

		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);

		editor.commit();
	}
	
	public static void putint(Context context, String key, Long value) {
		
		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(key, value);
		
		editor.commit();
	}
	
	public static void putintt(Context context, String key, int value) {
		
		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(key, value);
		
		editor.commit();
	}
	
	public static void putboolean(Context context, String key, Boolean value) {
		
		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		
		editor.commit();
	}

	/**
	 * 取出数据的方法
	 * 
	 * @param context
	 * @param key
	 * @return
	 */
	public static String getString(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);

		String arg = settings.getString(key, "0");
		return arg;
	}
	
	public static Long getLong(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);
		
		Long arg = settings.getLong(key, 0);
		return arg;
	}
	
	public static boolean getBoolean(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);
		
		boolean arg = settings.getBoolean(key, false);
		return arg;
	}
	public static int getInt(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);
		
		int arg = settings.getInt(key, 0);
		return arg;
	}

	
	/**
	 * 清除数据的方法
	 * 
	 * @param context
	 */
	public static void clear(Context context) {
		SharedPreferences settings = context.getSharedPreferences(datastore,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.clear();
		editor.commit();
	}

	/**
	 * 获取网络数据
	 * @param url_path
	 * @return
	 */
	public static String getJsonContent(String url_path) {
		try {
			URL url = new URL(url_path);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setConnectTimeout(3000);
			connection.setRequestMethod("GET");
			connection.setDoInput(true);
			int code = connection.getResponseCode();
			if (code == 200) {
				return changeInputStream(connection.getInputStream());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "";
	}

	private static String changeInputStream(InputStream inputStream) {
		// TODO Auto-generated method stub
		String jsonString = "";
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		int len = 0;
		byte[] data = new byte[1024];
		try {
			while ((len = inputStream.read(data)) != -1) {
				outputStream.write(data, 0, len);
			}
			jsonString = new String(outputStream.toByteArray());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonString;
	}
}
